import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { LoopSwitchGroupStateType, UseTaskGroupHardwareEnumType } from './typings'
import {
  getMonitorLoopSwitchGroupList,
  addMonitorLoopSwitchGroupInfo,
  updateMonitorLoopSwitchGroupInfo,
  deleteMonitorLoopSwitchGroupInfo,
  getMonitorGroupLoopList,
  getTaskGroupHardwareList,
  getNoTaskGroupHardwareList,
  getUseTaskGroupHardwareList,
  changeLoopGroupByLoopIds,
  getGroupLoopLogList,
  groupLoopApplyListPost,
  groupLoopRemoveApplyPost,
  getDeviceLoopTreeGet,
  getModelByDeviceIdGet,
  saveTaskGroupDeviceLoopPost
} from '~/api/monitorManagement/loopMonitorBox/loopSwitchGroup'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum } from '~/utils/util'

const loopSwitchGroup: Module<LoopSwitchGroupStateType, LoopSwitchGroupStateType> = {
  namespaced: true,
  state: {
    defaultLoopGroup: { id: undefined, groupName: '系统默认分组' },
    taskGroupHardwareSelectList: [],
    taskGroupHardwareEnum: {},
    useTaskGroupHardwareSelectList: [],
    noUseTaskGroupHardwareSelectList: [],
    loopSwitchSelectList: [],
    loopSwitchEnum: {},
    applyStateSelectList: [],
    applyStateEnum: {}
  },
  getters: {
    defaultLoopGroup: (state) => state.defaultLoopGroup,
    activeLoopGroupId: (state) => state.activeLoopGroupId,
    taskGroupHardwareSelectList: (state) => state.taskGroupHardwareSelectList,
    taskGroupHardwareEnum: (state) => state.taskGroupHardwareEnum,
    useTaskGroupHardwareSelectList: (state) => state.useTaskGroupHardwareSelectList,
    noUseTaskGroupHardwareSelectList: (state) => state.noUseTaskGroupHardwareSelectList,
    loopSwitchSelectList: (state) => state.loopSwitchSelectList,
    loopSwitchEnum: (state) => state.loopSwitchEnum,
    applyStateSelectList: (state) => state.applyStateSelectList,
    applyStateEnum: (state) => state.applyStateEnum
  },
  mutations: {
    SET_ACTIVE_LOOP_GROUP_ID(state, payload) {
      state.activeLoopGroupId = payload
    },
    SET_TASK_GROUP_HARDWARE_SELECT_LIST(state, payload) {
      const filterArr = (payload || [])?.concat([state.defaultLoopGroup])
      state.taskGroupHardwareSelectList = filterArr
      state.taskGroupHardwareEnum = filterValueEnum(
        filterArr,
        'id',
        'groupName'
      ) as LoopSwitchGroupStateType['taskGroupHardwareEnum']
    },
    SET_USE_TASK_GROUP_HARDWARE_SELECT_LIST(state, payload: UseTaskGroupHardwareEnumType[]) {
      state.useTaskGroupHardwareSelectList = payload
      state.noUseTaskGroupHardwareSelectList = payload?.filter((item) => !item?.isUsed)
    },
    SET_LOOP_SWITCH_SELECT_LIST(state, payload) {
      state.loopSwitchSelectList = payload
      state.loopSwitchEnum = filterValueEnum(
        payload,
        'dictKey',
        'dictValue'
      ) as LoopSwitchGroupStateType['loopSwitchEnum']
    },
    SET_APPLY_STATE_SELECT_LIST(state, payload) {
      state.applyStateSelectList = payload
      state.applyStateEnum = filterValueEnum(
        payload,
        'dictKey',
        'dictValue'
      ) as LoopSwitchGroupStateType['applyStateEnum']
    }
  },
  actions: {
    // 获取回路分组列表
    async GetMonitorLoopSwitchGroupList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getMonitorLoopSwitchGroupList<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 新增回路分组
    async AddMonitorLoopSwitchGroupInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await addMonitorLoopSwitchGroupInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 修改回路分组
    async UpdateMonitorLoopSwitchGroupInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await updateMonitorLoopSwitchGroupInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 删除回路分组
    async DeleteMonitorLoopSwitchGroupInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await deleteMonitorLoopSwitchGroupInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 应用
    async GroupLoopApplyListPost<S, T>(_: ActionContext<S, T>, params: T) {
      return await groupLoopApplyListPost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 删除
    async GroupLoopRemoveApplyPost<S, T>(_: ActionContext<S, T>, params: T) {
      return await groupLoopRemoveApplyPost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 回路tree
    async GetDeviceLoopTreeGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await getDeviceLoopTreeGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 获取设备型号
    async GetModelByDeviceIdGet<S, T extends { id?: string }>(_: ActionContext<S, T>, params: T) {
      return await getModelByDeviceIdGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 添加回路
    async SaveTaskGroupDeviceLoopPost<S, T>(_: ActionContext<S, T>, params: T) {
      return await saveTaskGroupDeviceLoopPost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 查询分组回路列表
    async GetMonitorGroupLoopList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getMonitorGroupLoopList<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 获取监控箱所有任务组
    async GetTaskGroupHardwareList<S, T>({ commit }: ActionContext<S, T>) {
      return await getTaskGroupHardwareList().then((res: AxiosResponse) => {
        commit('SET_TASK_GROUP_HARDWARE_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 获取监控箱无任务组
    async GetNoTaskGroupHardwareList<S, T>({ commit }: ActionContext<S, T>) {
      return await getNoTaskGroupHardwareList().then((res: AxiosResponse) => {
        commit('SET_TASK_GROUP_HARDWARE_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 查询监控箱任务组详情集合(是否分配任务)
    async GetUseTaskGroupHardwareList<S, T>({ commit }: ActionContext<S, T>) {
      return await getUseTaskGroupHardwareList().then((res: AxiosResponse) => {
        commit('SET_USE_TASK_GROUP_HARDWARE_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 回路变更分组
    async ChangeLoopGroupByLoopIds<S, T>(_: ActionContext<S, T>, params: T) {
      return await changeLoopGroupByLoopIds<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 获取分组回路任务历史
    async GetGroupLoopLogList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getGroupLoopLogList<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 查询回路开关数据字典
    async GetLoopSwitchSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'device_loop_name' }).then(
        (res: AxiosResponse) => {
          commit('SET_LOOP_SWITCH_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    },
    // 查询应用状态数据字典
    async GetApplyStateSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'device_apply_state' }).then(
        (res: AxiosResponse) => {
          commit('SET_APPLY_STATE_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    }
  }
}

export default loopSwitchGroup
